<template>
  <div class="app-container">
    <!-- 查询行 -->
    <el-row :gutter="10" class="mb8 query-row">
      <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
        <el-form-item label="实体编码" prop="code">
          <el-input v-model="queryParams.code" placeholder="请输入实体编码" clearable size="mini"
            @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="实体名称" prop="name">
          <el-input v-model="queryParams.name" placeholder="请输入实体名称" clearable size="mini"
            @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="实体类型" prop="type">
          <el-input v-model="queryParams.type" placeholder="请输入实体类型" clearable size="mini"
            @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="流程id" prop="flowId" v-show="showSearch">
          <el-input v-model="queryParams.flowId" placeholder="请输入流程id" clearable size="mini"
            @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="返回码" prop="retCode" v-show="showSearch">
          <el-input v-model="queryParams.retCode" placeholder="请输入返回码" clearable size="mini"
            @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="返回消息" prop="retMsg" v-show="showSearch">
          <el-input v-model="queryParams.retMsg" placeholder="请输入返回消息" clearable size="mini"
            @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="创建时间" v-show="showSearch">
          <el-date-picker v-model="dateRange" size="mini" style="width: 240px" value-format="yyyy-MM-dd" type="daterange"
            range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </el-row>
    <!-- 按钮行 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" :loading="loading"
          @click="handleExport" v-hasPermi="['aitcommon:EntityInfo:export']">导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-video-play" size="mini" :loading="loading"
          @click="handleRunFlow" :disabled="!currentRow">
          运行流程({{ currentRow && currentRow.flowCode }})
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-document" size="mini" :loading="loading" 
          @click="handleFlowInfo" :disabled="!currentRow">
          查看流程配置({{ currentRow && currentRow.id }})
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-document" size="mini" :loading="loading" 
          @click="handleRunTimeInfo" :disabled="!currentRow">
          查看运行信息
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain size="mini" :loading="loading" 
          @click="handleAITLog" :disabled="!currentRow">
          <svg-icon icon-class="log" /> 接口方法日志({{ currentRow && currentRow.flowId }})
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-tooltip content="清除所有错误数据,无法撤回!" placement="top-start" effect="dark">
          <el-button type="info" plain icon="el-icon-delete" size="mini" :loading="loading"
          @click="handleErrorClear">
          清除错误数据
          </el-button>
        </el-tooltip>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="EntityInfoList" @selection-change="handleSelectionChange"
      @row-dblclick="handleUpdate" :row-style="{ height: '32px' }" :cell-style="{ padding: '0px' }" border
      :height="tableHeight" :row-class-name="tableRowClassName" highlight-current-row
      @current-change="handleCurrentChange">
      <el-table-column label="ID" width="120" align="center" prop="id" fixed />
      <el-table-column label="实体编码" width="120" align="center" prop="code" fixed show-overflow-tooltip />
      <el-table-column label="实体名称" width="120" align="center" prop="name" fixed show-overflow-tooltip />
      <el-table-column label="实体类型" width="120" align="center" prop="type" show-overflow-tooltip />
      <el-table-column label="实体操作" width="80" align="center" prop="oper" show-overflow-tooltip />
      <el-table-column label="流程名称" width="120" align="center" prop="flowName" show-overflow-tooltip />
      <el-table-column label="同步时间" width="160" align="center" prop="createTime" />
      <el-table-column label="流程id" width="200" align="center" prop="flowId" />
      <el-table-column label="流程编码" width="200" align="center" prop="flowCode" show-overflow-tooltip />
      <el-table-column label="流程名称" width="200" align="center" prop="flowName" show-overflow-tooltip />
      <el-table-column label="返回码" width="60" align="center" prop="retCode" />
      <el-table-column label="返回消息" width="380" align="left" prop="retMsg" show-overflow-tooltip />
      <el-table-column label="是否归档" width="80" align="left" prop="isArchive" />
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改接口实体记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-tabs v-model="activeName">
          <el-tab-pane label="基本" name="base">
            <el-col :span="12">
              <el-form-item label="实体编码" prop="code">
                <el-input v-model="form.code" placeholder="请输入实体编码" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="实体名称" prop="name">
                <el-input v-model="form.name" placeholder="请输入实体名称" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="实体类型" prop="type">
                <el-input v-model="form.type" placeholder="请输入实体类型" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="实体操作" prop="oper">
                <el-input v-model="form.oper" placeholder="请输入实体操作" />
              </el-form-item>
            </el-col>
            
            <el-col :span="12">
              <el-form-item label="流程id" prop="flowId">
                <el-input v-model="form.flowId" placeholder="请输入流程id" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="流程编码" prop="flowCode">
                <el-input v-model="form.flowCode" placeholder="请输入流程编码" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="流程名称" prop="flowName">
                <el-input v-model="form.flowName" placeholder="请输入流程名称" />
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="返回码" prop="retCode">
                <el-input v-model="form.retCode" placeholder="请输入返回码" />
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="返回消息" prop="retMsg">
                <el-input v-model="form.retMsg" type="textarea" placeholder="请输入返回消息" :rows="3" />
              </el-form-item>
            </el-col>
          </el-tab-pane>
          <el-tab-pane label="参数" name="param">
            <el-col :span="24">
              <el-form-item label="原始数据" prop="dataJson">
                <el-input v-model="form.dataJson" type="textarea" placeholder="请输入内容" :rows="6" />
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="成功返回" prop="successJson">
                <el-input v-model="form.successJson" type="textarea" placeholder="请输入内容" :rows="6" />
              </el-form-item>
            </el-col>
          </el-tab-pane>
          <el-tab-pane label="其他" name="other">
            <el-col :span="12">
              <el-form-item label="参数字段0" prop="field0" v-show="showDetail">
                <el-input v-model="form.field0" placeholder="请输入参数字段0" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="参数字段1" prop="field1" v-show="showDetail">
                <el-input v-model="form.field1" placeholder="请输入参数字段1" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="参数字段2" prop="field2" v-show="showDetail">
                <el-input v-model="form.field2" placeholder="请输入参数字段2" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="参数字段3" prop="field3" v-show="showDetail">
                <el-input v-model="form.field3" placeholder="请输入参数字段3" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="参数字段4" prop="field4" v-show="showDetail">
                <el-input v-model="form.field4" placeholder="请输入参数字段4" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="参数字段5" prop="field5" v-show="showDetail">
                <el-input v-model="form.field5" placeholder="请输入参数字段5" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="参数字段6" prop="field6" v-show="showDetail">
                <el-input v-model="form.field6" placeholder="请输入参数字段6" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="参数字段7" prop="field7" v-show="showDetail">
                <el-input v-model="form.field7" placeholder="请输入参数字段7" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="参数字段8" prop="field8" v-show="showDetail">
                <el-input v-model="form.field8" placeholder="请输入参数字段8" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="参数字段9" prop="field9" v-show="showDetail">
                <el-input v-model="form.field9" placeholder="请输入参数字段9" />
              </el-form-item>
            </el-col>
          </el-tab-pane>
        </el-tabs>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <!-- <el-button type="primary" @click="submitForm">确 定</el-button> -->
        <el-button @click="cancel">取 消</el-button>
        <!-- <el-button @click="showDetailClick" v-show="!showDetail">详 细</el-button>
        <el-button @click="showDetailClick" v-show="showDetail">主 要</el-button> -->
      </div>
    </el-dialog>
    <!-- 查看流程配置,通过属性传入参数 -->
    <flow-info-dlg :visible.sync="flowOpen" :code="currentRow && currentRow.flowCode"></flow-info-dlg>
    <!-- 运行信息 -->
    <RunTimeDlg ref="RunTimeDlg"></RunTimeDlg>
  </div>
</template>

<script>
import {
  listEntityInfo,
  getEntityInfo,
  delEntityInfo,
  addEntityInfo,
  updateEntityInfo,
  exportEntityInfo,
  errorClear,
} from "@/api/aitcommon/EntityInfo";
import FlowInfoDlg from '@/components/AIT/FlowInfo';
import RunTimeDlg from '@/components/AIT/RunTimeDlg';
import { run } from "@/api/aitcommon/aitconfig";

export default {
  name: "EntityInfo",
  components: {FlowInfoDlg,RunTimeDlg},
  data() {
    return {
      // 日期范围
      dateRange: [],
      // 当前行
      currentRow: null,
      // 流程详情
      flowOpen:false,
      //表格高度
      tableHeight: 500,
      // 默认页签
      activeName: "base",
      // 表单详细
      showDetail: true,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: false,
      // 总条数
      total: 0,
      // 接口实体记录表格数据
      EntityInfoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 20,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },
  created() {
    // console.info('created')
    this.getList();
  },
  mounted() {
    this.$nextTick(() => {
      // window.innerHeight 浏览器窗口的可见高度，下面的 220 是除了table最大高度的剩余空间。
      let height = window.innerHeight - this.$refs.queryForm.$el.offsetHeight - 220;
      this.tableHeight = height;
    });
  },
  watch: {},
  methods: {
    handleAITLog() {
      if (!this.currentRow) {
        this.$message({
          message: '请先选择一条记录!',
          type: 'warning'
        });
        return;
      }
      // 跳转指定页面,带入查询参数
      this.$router.push({
        path: "/ait/aitlog",
        query: {
          flowId: this.currentRow.flowId
        }
      });
    },
    handleRunFlow() {
      if (this.currentRow && this.currentRow.flowCode) {
        this.loading = true;
        run(this.currentRow.flowCode).then(res => {
          // this.getList();
          // 显示执行结果
          // 改成异步调用,弹出窗体时可能尚未执行或还在执行
          this.$refs.RunTimeDlg.openDlg(this.currentRow.flowCode,this.getList);
        }).finally(res => {
          this.loading = false;
        })
      }
    },
    handleCurrentChange(val) {
      // console.info('handleCurrentChange',val)
      this.currentRow = val;
      // val && this.$refs.mainTable.toggleRowSelection(val);
    },
    handleErrorClear() {
      this.$confirm('将删除所有错误日志, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.loading = true;
        errorClear().then(res => {
          this.getList();
          this.msgSuccess('错误日志删除成功!');
        }).finally(res=>{
          this.loading = false;
        })
      })
    },
    handleRunTimeInfo() {
      if (!this.currentRow) {
        this.$message({
          message: '请先选择一条记录!',
          type: 'warning'
        });
        return;
      }
      // console.info('currentRow',this.currentRow)
      // 通过调用子控件方法,弹出窗口,并传递参数
      this.$refs.RunTimeDlg.openDlg(this.currentRow.flowCode,this.getList);
    },
    handleFlowInfo() {
      if (!this.currentRow) {
        this.$message({
          message: '请先选择一条记录!',
          type: 'warning'
        });
        return;
      }
      // console.info('handleFlowInfo',this.currentRow)
      this.flowOpen=true;
    },
    showDetailClick() {
      this.showDetail = !this.showDetail;
      //console.info('this.showDetail:',this.showDetail);
    },
    /** 查询接口实体记录列表 */
    getList() {
      // console.info('getList')
      this.loading = true;

      // 调用查询方法
      let query=this.addDateRange(this.queryParams, this.dateRange)
      listEntityInfo(query).then((response) => {
        // console.info("response", response.rows);
        this.EntityInfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {};
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum=1;

      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 20,
      };
      this.dateRange=[]
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加接口实体记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getEntityInfo(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改接口实体记录";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateEntityInfo(this.form).then((response) => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addEntityInfo(this.form).then((response) => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm(
        '是否确认删除接口实体记录编号为"' + ids + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return delEntityInfo(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有接口实体记录数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.loading = true;
          return exportEntityInfo(queryParams);
        })
        .then((response) => {
          this.download(response.msg);
          this.loading = false;
        })
        .catch(() => { });
    },
    tableRowClassName({ row, rowIndex }) {
      // console.info('tableRowClassName',row)
      if (row.retCode === '200') {
        return 'normal-row';
      } else {
        return 'abnormal-row';
      }
    },
  },
};
</script>

